<?php

/**
 * @copyright (C) 2008 by Slashes & Dots Sdn Bhd - All rights reserved!
 * @license http://www.azrul.com Copyrighted Commercial Software
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

require_once( JPATH_BASE .DS.'components' .DS.'com_community' .DS.'libraries' .DS.'core.php');

class plgCommunityMTree extends CApplications
{
	var $name 		= "My Listings in mTree";
	var $_name		= 'mTree';
	var $_path		= '';
	var $_user		= '';
	var $_my		= '';
	
	function plgCommunityMTree(& $subject, $config)
    {
		  $this->_user	=& CFactory::getActiveProfile();
		  $this->_my		=& CFactory::getUser();
		  $this->db 		= JFactory::getDBO();
  		parent::__construct($subject, $config);
   }
	
	
	function onProfileDisplay()
	{
		$config	=& CFactory::getConfig();
		$myJconfig =& JFactory::getConfig();
		
		$now = date( "Y-m-d H:i:s", time()+ $myJconfig->getValue('config.offset') *60*60 );
				
		/*
		$avatarModel		=& CFactory::getModel( 'avatar' );
		*/
		$user		= CFactory::getActiveProfile();
		$userName	= $user->getDisplayName();

				
		$db =& JFactory::getDBO();
		$db->setQuery("SELECT COUNT(*) FROM #__mt_links WHERE "
			. "\n	link_published='1' AND link_approved='1' AND user_id ='".$user->id."'"
			. "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now'  ) "
			. "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down >= '$now' ) "
			);
		$total_links = $db->loadResult();
		
	
		ob_start();
 		if( $total_links ){

			// Get latest listings
	        $maxCount = $this->params->get('count', 5);
			$db->setQuery( "SELECT l.*  FROM (#__mt_links AS l, #__mt_cl AS cl)"
			. "\n WHERE link_published='1' AND link_approved='1' AND user_id='".$user->id."' "
			. "\n AND l.link_id = cl.link_id AND cl.main = '1'"
			. "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now'  ) "
			. "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down >= '$now' ) "
			. "\n ORDER BY l.link_created DESC "
			."\n LIMIT 0, " . $maxCount);	

			$items = $db->loadObjectList();
		}else{
			$userId = "";
			$userName = ""; 
			$items = "";
		}
		
		$myItemid = '&amp;Itemid='.$this->getItemid();
		
    	$cache =& JFactory::getCache('plgCommunityMTree');
    	$cache->setCaching($this->params->get('cache', 1));
		$callback = array('plgCommunityMTree', '_getMTreeHTML');		
		$content = $cache->call($callback, $user->id, $userName, $items, $myItemid);
		return $content; 		
	}
	
	function _getMTreeHTML($userId, $userName, $items, $myItemid){
		ob_start();
 		
		if( !empty($items) ) {
				?>
				<div>
				<table width="100%">
				
				<?php
				foreach ($items as $item ){
						$myName		= $item->link_name;
						$myURL 		= JRoute::_("index.php?option=com_mtree&task=viewlink&link_id=".$item->link_id."&amp;Itemid=".$myItemid);               		 
            $myDate	 = new JDate($item->link_created);
						$myHits		= $item->link_hits; ?>
			            
					<ul class="list"><li>
					<tr>
						<td width="70%"><a href="<?php echo $myURL;?>"> <?php echo stripslashes ($myName); ?> </a></td>
						<td width="20%" align="left"><?php echo $myDate->toFormat(JText::_('DATE_FORMAT_LC3'));?></td>
						<td width="10%" align="right"><?php echo $myHits ?> hits</td>
					</tr>
					</li></ul><?php
				}?>
				</table>
				<div style="clear: both;"></div>
				</div>
				<?php
		} else {
				?>
		        <div class="content-nopost">
		            <?php echo $userName . ' ' . JText::_('hasn\'t contributed any listing yet'); ?>
		        </div>
				<?php
 		}
 		
		$contents = ob_get_contents();
		ob_end_clean();
		return $contents;
	}
	
	function onAppDisplay()
	{
		ob_start();
		$limit=0;
		$html= $this->onProfileDisplay($limit);
		echo $html;
		
		$content	= ob_get_contents();
		ob_end_clean(); 
	
		return $content;
		
	}
	
	/**
	 * Return itemid for mTree
	 */	 	
	function getItemid(){
		$db =& JFactory::getDBO();

       	$db->setQuery("SELECT id FROM #__menu WHERE link = 'index.php?option=com_mtree' AND published = 1");
       	$Itemid = $db->loadResult();
	
	    if ($Itemid < 1) {
			$Itemid = 0;
	    }
	    
	    return $Itemid;
	}
}